package com.base.api.controller.wxSign;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.base.utils.Redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WxTokenContext {

   private final RedisUtil redisUtil;

    public WxTokenContext(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    public String getTokeFromRedis(final String appId, final String secret){
        final String token = redisUtil.get(appId);
        if (StringUtils.isEmpty(token)){
            WXJsSdkToken wxJsSdkToken = getAccessTokenByCgiBin(appId, secret);
            String accessToken = wxJsSdkToken.getAccess_token();
            if (StringUtils.isNotEmpty(accessToken)){
                redisUtil.set(appId, accessToken, 3600L);
                log.warn("Token: {}", accessToken);
                return accessToken;
            }
        }
        log.warn("redis Token: {}", token);
        return token;
    }

    public WXAccessToken getAccessTokenByOauth2(final String appId, final String secret, final String code) throws Exception {
        log.warn("[getAccessTokenByOauth2]appId={} secret={} code={}", appId, secret, code);
        String tokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
                + appId + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
        String result = HttpRequest
                .post(tokenUrl)
                .header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
                .execute()
                .body();
        log.warn("result={}", result);
        WXAccessToken wxAccessToken = JSONObject.parseObject(result, WXAccessToken.class);
        return wxAccessToken == null ? new WXAccessToken() : wxAccessToken;
    }

    public WXJsSdkToken getAccessTokenByCgiBin(final String appId, final String secret){
        log.warn("[getAccessTokenByCgiBin]appId={} secret={}", appId, secret);
        String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + secret;
        try {
            String result = HttpRequest
                    .post(tokenUrl)
                    .header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
                    .execute()
                    .body();
            log.warn("result={}",result);
            WXJsSdkToken wxJsSdkToken = JSONObject.parseObject(result, WXJsSdkToken.class);
            return wxJsSdkToken == null ? new WXJsSdkToken() : wxJsSdkToken;
        }catch (Exception e){
            e.printStackTrace();
        }

        return new WXJsSdkToken();
    }

    public WXCgiBinUserInfo getUserInfo(String accessToken, String openId){
        log.warn("[getUserInfo]accessToken={} openId={}", accessToken, openId);

        String tokenUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="
                + accessToken + "&openid=" + openId + "&lang=zh_CN";
        try {
            String result = HttpRequest
                    .post(tokenUrl)
                    .header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
                    .execute()
                    .body();
            log.warn("result={}",result);
            WXCgiBinUserInfo wxCgiBinUserInfo = JSONObject.parseObject(result, WXCgiBinUserInfo.class);
            return wxCgiBinUserInfo == null ? new WXCgiBinUserInfo() : wxCgiBinUserInfo;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new WXCgiBinUserInfo();
    }
}
